﻿using ReservationModule.Classes;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ReservationModule
{
    public partial class CheckinPage : System.Web.UI.Page
    {
        decimal roomRate = 0;
        decimal tax = 0;
        decimal taxRate = Convert.ToDecimal(17.7 / 100);
        decimal roomCharges = 0;
        int numberOfRooms = 0;
        int nights = 0;
        decimal total = 0;
        int customerID = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {

            Session["Login"] = Session["Login"];
            if (Session["Login"] == "false" || Session["Login"] == null)
            {
                Response.Redirect("Reject.aspx");
            }

            if (!IsPostBack)
            {
                //"Select Dates" is selected by default
                tabDatesNights.CssClass = "Clicked";
                MainView.ActiveViewIndex = 0;

                //auto add all countries' names to drop down list
                Dictionary<string, string> objDic = new Dictionary<string, string>();
                foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
                {
                    RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);
                    if (!objDic.ContainsKey(objRegionInfo.EnglishName))
                    {
                        objDic.Add(objRegionInfo.EnglishName, objRegionInfo.EnglishName);
                    }
                }

                var obj = objDic.OrderBy(p => p.Key);
                foreach (KeyValuePair<string, string> val in obj)
                {
                    ddlCountry.Items.Add(new ListItem(val.Key, val.Value));
                }

                //set minimum credit card expiry month and year to current
                string currentMonth = DateTime.Today.Month.ToString();
                int currentYear = DateTime.Today.Year;
                ddlExpiryMonth.Text = Convert.ToString(currentMonth);
                ddlExpiryYear.Text = Convert.ToString(currentYear);
            }

            //prevent selection of past dates
            tbxCheckInDate_CalendarExtender.StartDate = DateTime.Today;
            tbxCheckOutDate_CalendarExtender.StartDate = DateTime.Today.AddDays(1);

            //confirmation page
            lblCheckInDate.Text = tbxCheckInDate.Text;
            lblCheckOutDate.Text = tbxCheckOutDate.Text;
            lblNights.Text = ddlNights.Text;
            lblAdults.Text = ddlAdults.Text;
            lblChildren.Text = ddlChildren.Text;
            lblRoomType.Text = rblRoomTypes.Text;
            lblRoomRate.Text = tbxRoomRates.Text;
            lblNumberOfRooms.Text = rblNumberOfRooms.Text;
            lblRoomCharges.Text = tbxRoomCharges.Text;
            lblTax.Text = tbxTax.Text;
            lblTotal.Text = tbxTotal.Text;
            lblPaymentType.Text = ddlPaymentTypes.Text;
            lblCreditCardNumber.Text = tbxCreditCardNumber.Text;
            lblNameOfCardHolder.Text = tbxNameOfCardHolder.Text;
            lblExpiryMonth.Text = ddlExpiryMonth.Text;
            lblExpiryYear.Text = ddlExpiryYear.Text;
            lblFirstName.Text = tbxFirstName.Text;
            lblLastName.Text = tbxLastName.Text;
            lblNRIC.Text = tbxNRIC.Text;
            lblGender.Text = rblGender.Text;
            lblDateOfBirth.Text = tbxDOB.Text;
            lblNationality.Text = tbxNationality.Text;
            lblAddress1.Text = taAddress.InnerText;
            lblCity.Text = tbxCity.Text;
            lblCountry.Text = ddlCountry.SelectedValue;
            lblPostalCode.Text = tbxPostalCode.Text;
            lblContactNumber.Text = tbxContactNumber.Text;
            lblEmail.Text = tbxEmail.Text;
            taCfmComments.InnerText = taComments.InnerText;

            
        }

        protected void tabDatesNights_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Clicked";
            tabRoomsRates.CssClass = "Initial";
            tabPersonalDetails.CssClass = "Initial";
            tabPaymentDetails.CssClass = "Initial";
            tabConfirmation.CssClass = "Initial";
            MainView.ActiveViewIndex = 0;
        }

        protected void tabRoomsRates_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Initial";
            tabRoomsRates.CssClass = "Clicked";
            tabPersonalDetails.CssClass = "Initial";
            tabPaymentDetails.CssClass = "Initial";
            tabConfirmation.CssClass = "Initial";
            tabDatesNights.Enabled = Convert.ToBoolean("True");
            MainView.ActiveViewIndex = 1;
        }

        protected void tabPersonalDetails_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Initial";
            tabRoomsRates.CssClass = "Initial";
            tabPersonalDetails.CssClass = "Clicked";
            tabPaymentDetails.CssClass = "Initial";
            tabConfirmation.CssClass = "Initial";
            tabDatesNights.Enabled = Convert.ToBoolean("True");
            tabRoomsRates.Enabled = Convert.ToBoolean("True");
            MainView.ActiveViewIndex = 2;
        }
        protected void tabPaymentDetails_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Initial";
            tabRoomsRates.CssClass = "Initial";
            tabPersonalDetails.CssClass = "Initial";
            tabPaymentDetails.CssClass = "Clicked";
            tabConfirmation.CssClass = "Initial";
            tabDatesNights.Enabled = Convert.ToBoolean("True");
            tabRoomsRates.Enabled = Convert.ToBoolean("True");
            tabPaymentDetails.Enabled = Convert.ToBoolean("True");
            MainView.ActiveViewIndex = 3;
        }

        protected void tabConfirmation_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Initial";
            tabRoomsRates.CssClass = "Initial";
            tabPersonalDetails.CssClass = "Initial";
            tabPaymentDetails.CssClass = "Initial";
            tabConfirmation.CssClass = "Clicked";
            tabDatesNights.Enabled = Convert.ToBoolean("True");
            tabRoomsRates.Enabled = Convert.ToBoolean("True");
            tabPersonalDetails.Enabled = Convert.ToBoolean("True");
            tabPaymentDetails.Enabled = Convert.ToBoolean("True");
            MainView.ActiveViewIndex = 4;
        }

        protected void btnContinue1_Click(object sender, EventArgs e)
        {
            if (lblResult.Visible == false && tbxSearch.Text != "")
            {
                tabDatesNights.CssClass = "Initial";
                tabRoomsRates.CssClass = "Clicked";
                tabPersonalDetails.CssClass = "Initial";
                tabPaymentDetails.CssClass = "Initial";
                tabConfirmation.CssClass = "Initial";
                tabDatesNights.Enabled = Convert.ToBoolean("True");
                MainView.ActiveViewIndex = 1;

                string[] roomTypes = { "Single", "Double", "Suite" };
                Room r = null;
                foreach (string i in roomTypes)
                {
                    if (rblRoomTypes.Text == i)
                    {
                        r = DBManager.GetRoomRateByRoomType(i);
                        break;
                    }
                }

                if (r != null)
                {
                    roomRate = r.RoomRate1;
                    tbxRoomRates.Text = roomRate.ToString("F");
                }

                numberOfRooms = Convert.ToInt32(rblNumberOfRooms.Text);
                nights = Convert.ToInt32(ddlNights.Text);
                roomCharges = roomRate * numberOfRooms * nights;
                tbxRoomCharges.Text = roomCharges.ToString("F");
                tax = roomCharges * taxRate;
                tbxTax.Text = tax.ToString("F");
                total = roomCharges + tax;
                tbxTotal.Text = total.ToString("F");
            }
           
        }

        protected void btnContinue2_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Initial";
            tabRoomsRates.CssClass = "Initial";
            tabPersonalDetails.CssClass = "Clicked";
            tabPaymentDetails.CssClass = "Initial";
            tabConfirmation.CssClass = "Initial";
            tabDatesNights.Enabled = Convert.ToBoolean("True");
            tabRoomsRates.Enabled = Convert.ToBoolean("True");
            MainView.ActiveViewIndex = 2;
        }

        protected void btnContinue3_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Initial";
            tabRoomsRates.CssClass = "Initial";
            tabPersonalDetails.CssClass = "Initial";
            tabPaymentDetails.CssClass = "Clicked";
            tabConfirmation.CssClass = "Initial";
            tabDatesNights.Enabled = Convert.ToBoolean("True");
            tabRoomsRates.Enabled = Convert.ToBoolean("True");
            tabPersonalDetails.Enabled = Convert.ToBoolean("True");
            MainView.ActiveViewIndex = 3;
        }

        protected void btnContinue4_Click(object sender, EventArgs e)
        {
            tabDatesNights.CssClass = "Initial";
            tabRoomsRates.CssClass = "Initial";
            tabPersonalDetails.CssClass = "Initial";
            tabPaymentDetails.CssClass = "Initial";
            tabConfirmation.CssClass = "Clicked";
            tabDatesNights.Enabled = Convert.ToBoolean("True");
            tabRoomsRates.Enabled = Convert.ToBoolean("True");
            tabPersonalDetails.Enabled = Convert.ToBoolean("True");
            tabPaymentDetails.Enabled = Convert.ToBoolean("True");
            MainView.ActiveViewIndex = 4;
        }

        protected void tbxCheckInDate_TextChanged(object sender, EventArgs e)
        {
            DateTime checkinDate = Convert.ToDateTime(tbxCheckInDate.Text);
            int nights = Convert.ToInt32(ddlNights.SelectedItem.Value);
            checkinDate = checkinDate.AddDays(nights);
            tbxCheckOutDate.Text = checkinDate.ToString("dddd, MMMM dd, yyyy");
        }

        protected void tbxCheckOutDate_TextChanged(object sender, EventArgs e)
        {
            DateTime checkoutDate = Convert.ToDateTime(tbxCheckOutDate.Text);
            DateTime checkinDate = Convert.ToDateTime(tbxCheckInDate.Text);
            int difference = Convert.ToInt32(ddlNights.Text);
            checkinDate = checkoutDate.AddDays(-nights);
            int result = DateTime.Compare(checkinDate, DateTime.Today);
            if (result <= 0)
            {
                tbxCheckInDate.Text = checkinDate.ToString("dddd, MMMM dd, yyyy");
                ddlNights.Text = Convert.ToString((checkoutDate - checkinDate).TotalDays);
            }
            else
            {
                checkinDate = DateTime.Today;
                tbxCheckInDate.Text = checkinDate.ToString("dddd, MMMM dd, yyyy");
                ddlNights.Text = Convert.ToString((checkoutDate - checkinDate).TotalDays);
            }
        }

        protected void ddlNights_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime checkinDate = Convert.ToDateTime(tbxCheckInDate.Text);
            int nights = Convert.ToInt32(ddlNights.SelectedItem.Value);
            checkinDate = checkinDate.AddDays(nights);
            tbxCheckOutDate.Text = checkinDate.ToString("dddd, MMMM dd, yyyy");
        }

        protected void rblRoomTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] roomTypes = { "Single", "Double", "Suite" };
            Room r = null;
            foreach (string i in roomTypes)
            {
                if (rblRoomTypes.Text == i)
                {
                    r = DBManager.GetRoomRateByRoomType(i);
                    break;
                }
            }

            if (r != null)
            {
                roomRate = r.RoomRate1;
                tbxRoomRates.Text = roomRate.ToString("F");
            }

            numberOfRooms = Convert.ToInt32(rblNumberOfRooms.Text);
            nights = Convert.ToInt32(ddlNights.Text);
            roomCharges = roomRate * numberOfRooms * nights;
            tbxRoomCharges.Text = roomCharges.ToString("F");
            tax = roomCharges * taxRate;
            tbxTax.Text = tax.ToString("F");
            total = roomCharges + tax;
            tbxTotal.Text = total.ToString("F");
        }

        protected void rblNumberOfRooms_SelectedIndexChanged(object sender, EventArgs e)
        {
            numberOfRooms = Convert.ToInt32(rblNumberOfRooms.Text);
            nights = Convert.ToInt32(ddlNights.Text);
            roomRate = Convert.ToDecimal(tbxRoomRates.Text);
            roomCharges = roomRate * numberOfRooms * nights;
            tbxRoomCharges.Text = roomCharges.ToString("F");
            tax = roomCharges * taxRate;
            tbxTax.Text = tax.ToString("F");
            total = roomCharges + tax;
            tbxTotal.Text = total.ToString("F");
        }

        protected void btnCheckIn_Click(object sender, EventArgs e)
        {
            string creditCarNumber = lblCreditCardNumber.Text;
            string expiryDate = lblExpiryMonth.Text + lblExpiryYear.Text;
            string name = lblNameOfCardHolder.Text;
            string creditCardType = ddlPaymentTypes.Text;
             customerID = Convert.ToInt32(tbxSearch.Text);
            CreditCard cc = new CreditCard() { CreditCardNumber1 = creditCarNumber, ExpiryDate1 = expiryDate, NameOfCardHolder1 = name, CreditCardType1 = creditCardType, CustomerID1 = customerID};
            int row = DBManager.UpdateCreditCard(cc);

            string nric = lblNRIC.Text;
            string firstName = lblFirstName.Text;
            string lastName = lblLastName.Text;
            string postalCode = lblPostalCode.Text;
            string city = lblCity.Text;
            string country = ddlCountry.Text;
            string nationality = tbxNationality.Text;
            string contactNumber = lblContactNumber.Text;
            string address = lblAddress1.Text + " " + lblAddress2.Text + " " + lblAddress3.Text;
            string email = lblEmail.Text;
            string gender = lblGender.Text;
            DateTime dob = Convert.ToDateTime(tbxDOB.Text);
            string additionalComment = taCfmComments.InnerText;

            Customer cu = new Customer() { CustomerID1 = customerID, NRIC1 = nric, FirstName1 = firstName, LastName1 = lastName, PostalCode1 = postalCode, City1 = city, Country1 = country, Nationality1 = nationality, ContactNumber1 = contactNumber, Address1 = address, Email1 = email, Gender1 = gender, DateOfBirth1 = dob, AdditionalComment1 = additionalComment };
            int customerRows = DBManager.UpdateCustomer(cu);

            DateTime checkInDate = Convert.ToDateTime(lblCheckInDate.Text);
            DateTime checkOutDate = Convert.ToDateTime(lblCheckOutDate.Text);
            int numberOfGuestAdults = Convert.ToInt32(lblAdults.Text);
            int numberOfGuestChildren = Convert.ToInt32(lblChildren.Text);
            int numberOfNights = Convert.ToInt32(lblNights.Text);
            string reservationStatus = "Checked In";
            string additionalremarks = taCfmComments.InnerText;
            int numberOfRoom = Convert.ToInt32(lblNumberOfRooms.Text);
            Reservation re = new Reservation() { CustomerID1 = customerID, DesiredCheckInDate1 = checkInDate, DesiredCheckOutDate1 = checkOutDate, NumberOfGuestAdults1 = numberOfGuestAdults, NumberOfGuestChildren1 = numberOfGuestChildren, NumberOfNights1 = numberOfNights, ReservationStatus1 = reservationStatus, AdditionalRemarks1 = additionalremarks, NumberOfRooms1 = numberOfRoom};
            int reservationRows = DBManager.UpdateReservation(re);

            int resID = Convert.ToInt32(tbxHiddenID.Text);
            Room r = new Room() { RoomStatus1 = "Vacant", ReservationID1 = resID };
            int roomRow = DBManager.UpdateRoomStatus(r);

            string roomtype = lblRoomType.Text;
            int numberofrooms = Convert.ToInt32(lblNumberOfRooms.Text);
            Reservation re3 = new Reservation() { NumberOfRooms1 = numberofrooms, ReservationID1 = resID };
            Room r1 = new Room() { RoomStatus1 = "Occupied", RoomType1 = roomtype, ReservationID1 = resID };
            int roomRow2 = DBManager.UpdateRoom(r1, re3);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int reservationID = 0;
            
            customerID = Convert.ToInt32(tbxSearch.Text);
            Reservation re = DBManager.GetReservationByCustomerID(customerID);
            if (re != null)
            {
                reservationID = re.ReservationID1;
                tbxHiddenID.Text = Convert.ToString(re.ReservationID1);
                tbxCheckInDate.Text = re.DesiredCheckInDate1.ToString("dddd, MMMM dd, yyyy");
                tbxCheckOutDate.Text = re.DesiredCheckOutDate1.ToString("dddd, MMMM dd, yyyy");
                ddlNights.Text = Convert.ToString(re.NumberOfNights1);
                ddlAdults.Text = Convert.ToString(re.NumberOfGuestAdults1);
                ddlChildren.Text = Convert.ToString(re.NumberOfGuestChildren1);
                rblNumberOfRooms.Text = Convert.ToString(re.NumberOfRooms1);
            }
            
            Room r = DBManager.GetRoomByReservationID(reservationID);
            if (r != null)
            {
                rblRoomTypes.Text = r.RoomType1;
                tbxRoomRates.Text = r.RoomRate1.ToString("F");
            }

            numberOfRooms = Convert.ToInt32(rblNumberOfRooms.Text);
            nights = Convert.ToInt32(ddlNights.Text);
            roomCharges = roomRate * numberOfRooms * nights;
            tbxRoomCharges.Text = roomCharges.ToString("F");
            tax = roomCharges * taxRate;
            tbxTax.Text = tax.ToString("F");
            total = roomCharges + tax;
            tbxTotal.Text = total.ToString("F");
            
            Customer cu = DBManager.GetCustomerByCustomerID(customerID);
            if (cu != null)
            {
                lblResult.Visible = false;
                tbxFirstName.Text = cu.FirstName1;
                tbxLastName.Text = cu.LastName1;
                tbxNRIC.Text = cu.NRIC1;
                rblGender.Text = cu.Gender1;
                tbxDOB.Text = cu.DateOfBirth1.ToString("dddd, MMMM dd, yyyy");
                tbxNationality.Text = cu.Nationality1;
                taAddress.InnerText = cu.Address1;
                tbxCity.Text = cu.City1;
                ddlCountry.SelectedValue = cu.Country1;
                tbxPostalCode.Text = cu.PostalCode1;
                tbxContactNumber.Text = cu.ContactNumber1;
                tbxEmail.Text = cu.Email1;
                taComments.InnerText = cu.AdditionalComment1;
            }

            else
            {
                lblResult.Visible = true;
                lblResult.Text = "Please enter an existing Customer ID.";
            }

            CreditCard cc = DBManager.GetCreditCardByCustomerID(customerID);
            if (cc != null)
            {
                ddlPaymentTypes.Text = cc.CreditCardType1;
                tbxCreditCardNumber.Text = cc.CreditCardNumber1;
                tbxNameOfCardHolder.Text = cc.NameOfCardHolder1;
                DateTime expiryDate = Convert.ToDateTime(cc.ExpiryDate1);
                string month = expiryDate.ToString("MMMM");
                ddlExpiryMonth.Text = month;
                string year = expiryDate.ToString("yyyy");
                ddlExpiryYear.Text = year;
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("BookingMenu.aspx");
        }
    }
}
